/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package realisateur.gestionnaires;

import java.util.Collection;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import realisateur.modeles.Realisateur;

/**
 *
 * @author hlaffineur
 */
@Stateless
@LocalBean
public class GestionnaireRealisateur {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext  
    private EntityManager em;
    
    public Realisateur creerReal(String lastname, String firstname, String datebirth, String descr){
        Realisateur r = new Realisateur(lastname,firstname,datebirth,descr);
        em.persist(r);
        return r;
    }
    
    public void peupleLaBaseRealAvecJeuDeTest(){
        creerReal("Wachowski", "Andy", "1967", "American");
        creerReal("Wachowski", "Larry", "1965", "American");
        creerReal("Lucas", "Georges", "1944", "American");
        creerReal("Cameron", "James", "1954", "Canadian, won 8 prices");
        creerReal("Kershner", "Irvin", "1923", "American, passed away");
        creerReal("Marquand", "Richard", "1938", "American, passed away");
        creerReal("Fletcher", "Anne", "1966", "American");
        creerReal("Peli", "Oren", "1971", "Israelian");
        creerReal("Sonnenfeld", "Barry", "1953", "American");
        creerReal("Wedge", "Chris", "1957", "American");
        creerReal("Saldanha", "Carlos", "1968", "Brazilian");
        creerReal("Adamson", "Andrew", "1966", "American");
        creerReal("Jenson", "Victoria", "1960", "American");
        creerReal("Forman", "Milos", "1932", "Tchecoslovakian");
        creerReal("Redford", "Robert", "1936", "Tchecoslovakian");
        creerReal("Darabont", "Frank", "1959", "French");
        creerReal("Bay", "Michael", "1965", "American");
        creerReal("Besson", "Luc", "1959", "French");
        creerReal("Peterson", "Wolfgang", "1941", "German");
    }
    
    public Collection<Realisateur> getAllRealisateur() {
        Query q = em.createQuery("select r from Realisateur r");
        return q.getResultList();
    }
    
    
    public Realisateur getRealisateur(int id) {
        Query q = em.createQuery("select r from Realisateur r where r.id = '" + id + "'");
        System.out.println("La requete marche");
        return (Realisateur)q.getSingleResult();
    }
    
    
    public Realisateur getRealisateur(String nom, String prenom) {
        Query q = em.createQuery("select r from Realisateur r where r.nom = '" + nom + "' and r.prenom = '" + prenom + "'");
        System.out.println("La requete marche");
        return (Realisateur)q.getSingleResult();
    }
    
}
